<script setup lang='ts' name="protocol-management">
import { useGetManBuyerOrderPage } from '@fl/api/man-api'
import { SearchForm } from '@fl/components'
import { cloneDeep } from 'lodash-es'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

import { useForm } from './hooks/use-form'
import ListItem from './list.vue'

const router = useRouter()

const { form, reset, schema } = useForm()

const pageInfo = ref<any>({
    page: 1,
    size: 10,
    total: 0,
})

const searchParams = computed(() => {
    return {
        ...form.value,
        orderTimeEnd: form.value.selectTime?.[1],
        orderTimeStart: form.value.selectTime?.[0],
        page: pageInfo.value.page,
        selectTime: '',
        size: pageInfo.value.size,
    }
})
const { data, isLoading } = useGetManBuyerOrderPage(searchParams)

const dataList = ref([])

const show = ref(false)
const showApproval = ref(false)
const showInfo = ref(false)

function doAction(item: any, row: any) {
    console.log('doAction', item.value)
    switch (item.value) {
        case 'send':
            console.log('立即发货', row)
            show.value = true
            break
        case 'edit':
            console.log('修改订单', row)
            router.push('/purchase-pages/purchase-order-management/edit-order')
            break
        case 'view':
            console.log('查看并接单', row)
            router.push('/purchase-pages/purchase-order-management/purchase-sales-order-view')
            break
        case 'log':
            router.push('/purchase-pages/purchase-order-management/purchase-sales-order-log')
            break
        case 'approval':
            console.log('申请验收', row)
            showApproval.value = true
            break
        case 'detail':
            showInfo.value = true
            console.log('申请验收单明细', row)
            break
        default:
            break
    }
}
function submit(val: any) {
    form.value = {
        ...val,
    }
}
function pageChange(val: any) {
    pageInfo.value = Object.assign(pageInfo.value, val)
}
watchEffect(() => {
    if (data.value) {
        dataList.value = cloneDeep(data.value?.records ?? [])
        dataList.value.forEach((item: any) => {
            item.expand = true
        })
        pageInfo.value.total = data.value?.total ?? 0
    }
})
</script>

<template>
    <ElCard mb-20
            class="top-32"
    >
        <SearchForm v-bind="form"
                    :columns="schema"
                    submit-text="查询"
                    @submit="submit"
                    @reset="reset"
        />
    </ElCard>

    <ElCard mb-20>
        <div class="tool_btn" />

        <ListItem :data="dataList"
                  :total="pageInfo.total"
                  :loading="isLoading"
                  @do-action="doAction"
                  @page-change="pageChange"
        />
    </ElCard>
</template>

<style lang="less" scoped>
.tool_btn {
    margin-bottom: 20px;
}

.top-32 {
    margin-top: -32px;
}
</style>
